<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.LinkedList"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@ page import="com.google.appengine.api.datastore.Query"%>
<%@ page import="com.google.appengine.api.datastore.Entity"%>
<%@ page import="com.google.appengine.api.datastore.FetchOptions"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@ page import="com.google.appengine.api.datastore.Text"%>
<%@ page import="com.google.appengine.api.oauth.OAuthRequestException"%>
<%@ page import="com.google.appengine.api.oauth.OAuthService"%>
<%@ page import="com.google.appengine.api.oauth.OAuthServiceFactory"%>
<%@ page import="com.google.appengine.api.oauth.OAuthServiceFailureException"%>
<%@ page import="com.google.appengine.api.datastore.Query.FilterOperator"%>
<%@ page import="java.util.Date"%>
<%@ page import="edu.dhbw.mannheim.Admin"%>
<%@ page import="edu.dhbw.mannheim.Comment"%>
<%@ page import="edu.dhbw.mannheim.NameDate"%>
<!-- show post -->
<%

	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser(); 
	
	// get the id of the URL
	String blogId = request.getParameter("id");
	int blogIdInt = 0;
	if (blogId != null) {
		try{
			blogIdInt = Integer.parseInt(blogId);
			if (blogIdInt < 0) {
				blogIdInt = -1;
			}
		} catch(NumberFormatException e) {
			blogIdInt=-1;
		}
	} else {
		blogIdInt = -1;
	}
	
	//set a string to the search field
	if(request.getParameter("search") != null) 
	{
		%>
		<script>
	   	 	document.getElementById("searchInput").value = "<%=request.getParameter("search")%>"
	   	 	search("<%=request.getParameter("search")%>");
		</script>
		<%
	}
				
	
	
	// connect to the database
	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	Key blogKey = KeyFactory.createKey("blog", "blogblogblog");

	// query to get the blog with the id from the url
	Query query = new Query("post", blogKey).addFilter("internalID", Query.FilterOperator.EQUAL, blogIdInt + "");

	//query for all blogs
	Query query2 = new Query("post", blogKey).addSort("date", Query.SortDirection.DESCENDING);
	
	//declarations
	List<Entity> posts = new LinkedList<Entity>();
	int offset = 1;
	
	//get the blog with the id from the url
	Entity post = datastore.prepare(query).asSingleEntity();
	
	//get the last blog if the one from the URL does not exist
	if (post == null) {
		posts = datastore.prepare(query2).asList(FetchOptions.Builder.withLimit(1));
	
		//catch if the blog is empty
		if (posts.isEmpty()) {
			posts = null;
			%>
			<p>This blog has no messages.</p>
			<%
		} else {
			
				post = posts.get(0);
				blogIdInt=Integer.parseInt(post.getProperty("internalID").toString());
		}
	}
	if (posts != null) {
		%>
		<jsp:include page="navBar.jsp">
			<jsp:param name="blogIdInt" value="<%=blogIdInt%>" />
		</jsp:include>
		
		<%=NameDate.getNameDateHTML(post)%>
		<% //content of the blog %>
		<div id="postDiv" class="inner round">
			<div id="plusonediv" class="floatright"><g:plusone></g:plusone></div>
			<h1 id="topic"><%=(String) post.getProperty("topic")%></h1>
			<blockquote id="postContent"><%=((Text) post.getProperty("content")).getValue()%></blockquote>
			<% if(user != null && Admin.canPost(user.getEmail())) { %>
			<div id="editButton" class="floatright"><input class="small button round" type="button" onClick="editPost()" value="edit" /></div>
			<%} %>
		</div>

		<% //comments %>
		<div id="comments">
		
			<% //inserting a new Comment to a blog %>
		<form>
			<div>
				<input id="blogIdInt" type="hidden" name="blogIdInt"
					value="<%=blogIdInt%>"> Insert your comment here:
				<div class="textarea round">
					<textarea id="commentField" class="commentEditor textarea fullwidth"
						name="content" rows="5" cols="80"></textarea>
				</div>
			</div>
			<div class="center">
				<input class="small button round" type="button" onclick="newComment()"
					value="Post Comment" />
			</div>
		</form>
		
		<%
		//generate the String with the amount of comments
		Query blogComments = new Query("comment").addSort("date",Query.SortDirection.DESCENDING);
		blogComments.setAncestor(post.getKey());
		int amountComments = datastore.prepare(blogComments).countEntities(FetchOptions.Builder.withDefaults());
		%><div id="commentAmountString"><%
			if (amountComments == 1) {
				%>
				There is one comment:
				<%
			} else if (amountComments != 0) {
				%>
				There are <span id="commentAmount"><%=amountComments%></span> comments:
				<%
			}
		%> </div>
		
			<div id="commentsList"><%=Comment.getCommentsAsHTML(blogIdInt,0,10)%></div>
		</div>
		
		<jsp:include page="navBar.jsp">
			<jsp:param name="blogIdInt" value="<%=blogIdInt%>" />
		</jsp:include>
<%
	}
%>
